import axios from "axios";

export default {
  state: () => ({
    todos: []
  }),
  getters: {},
  mutations: {
    SET_TODOS (state, data) {
      state.todos = data
    },
  },
  actions: {
    async fetchList({commit}) {
      const res = await axios({
        url: 'https://api.yizhanketang.cn/api/v1/todos'
      })
      const { data } = res.data
      commit('SET_TODOS', data)
    },
    async createTodo({dispatch}, data) {
      await axios({
        method: 'POST',
        url: 'https://api.yizhanketang.cn/api/v1/todos',
        data: {
          content: data
        }
      })
      await dispatch('fetchList')
    },
    async completedTodo({dispatch}, data) {
      await axios({
        method: 'POST',
        url: `https://api.yizhanketang.cn/api/v1/todos/${data.id}/completed`,
        data: {
          completed: data.completed
        }
      })
      await dispatch('fetchList')
    }
  },
  namespaced: true,
}

